package com.lysj.admin.master.bussiness.company.recharge.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lysj.admin.master.bussiness.admin.domain.CompanyProviderBind;
import com.lysj.admin.master.bussiness.admin.service.CompanyProviderBindService;
import com.lysj.admin.master.bussiness.company.company.dto.CompanyBaseDto;
import com.lysj.admin.master.bussiness.company.recharge.domain.RechargeRecord;
import com.lysj.admin.master.bussiness.company.recharge.mapper.RechargeRecordMapper;
import com.lysj.admin.master.bussiness.company.recharge.service.RechargeRecordService;
import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.common.annotation.MyList;
import com.lysj.admin.master.common.annotation.Save;
import com.lysj.admin.master.common.enumeration.EnumUtils;
import com.lysj.admin.master.common.spring.mpbase.MyController;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.common.spring.pagination.PageRequestDTO;
import com.lysj.admin.master.common.web.ParamUtil;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Created by drj on 2020-01-06 16:43
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/company/recharge_record")
public class RechargeRecordController extends MyController<RechargeRecordMapper, RechargeRecord, PageRequestDTO> {

    @Resource
    private RechargeRecordService rechargeRecordService;
    @Resource
    private CompanyProviderBindService companyProviderBindService;

    @Override
    public MyService<RechargeRecordMapper, RechargeRecord> getService() {
        return rechargeRecordService;
    }


    @Override
    @Save
    public Resp save(RechargeRecord entity) {
        //获取当前登录公司ID
        String companyId = getJwtProperty(CommonConstant.COMPANY_ID_NAME);
        if (ParamUtil.isBlank(entity.getServiceProviderId())) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "请选择服务商");
        }
        CompanyProviderBind companyProviderBind = companyProviderBindService.findByCompanyIdAndServiceProviderId(companyId, entity.getServiceProviderId());
        if (ParamUtil.isBlank(companyProviderBind.getFeeRate())) {
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "请先完善该服务商费率配置");
        }
        entity.setCompanyId(companyId);
        entity.setRechargeStatus(RechargeRecord.RechargeStatus.AUDITING.getCode());
        return super.save(entity);
    }

    /*
     * @author Created by drj on 2020/1/6 16:53
     * @Description :管理员获取充值记录
     */
    @Override
    @MyList
    public Resp list(PageRequestDTO condition) {
        Page<RechargeRecord> rechargeRecordPage = getService().pageList(condition);
        for (RechargeRecord rechargeRecord : rechargeRecordPage.getRecords()) {
            rechargeRecord.setRechargeStatusTxt(EnumUtils.findStatusByCode(rechargeRecord.getRechargeStatus(),
                    RechargeRecord.RechargeStatus.class));
        }
        return RespUtil.success(rechargeRecordPage);
    }


    @GetMapping("/find_by_company_id")
    public Resp findByCompanyId(CompanyBaseDto condition) {
        String companyId = getJwtProperty(CommonConstant.COMPANY_ID_NAME);
        condition.setCompanyId(companyId);
        Page<RechargeRecord> rechargeRecordPage = getService().pageList(condition);
        for (RechargeRecord rechargeRecord : rechargeRecordPage.getRecords()) {
            rechargeRecord.setRechargeStatusTxt(EnumUtils.findStatusByCode(rechargeRecord.getRechargeStatus(),
                    RechargeRecord.RechargeStatus.class));
        }
        return RespUtil.success(rechargeRecordPage);
    }

    @PostMapping("/audit")
    public Resp audit(String id, Integer rechargeStatus) {
        return RespUtil.success(rechargeRecordService.audit(id, rechargeStatus));
    }

}
